package org.edu.modules.logistics.ct.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.edu.common.base.BasePageDto;
import org.edu.common.base.BaseServiceImpl;
import org.edu.common.utils.ConvertUtils;
import org.edu.common.base.BaseServiceImpl;
import org.edu.modules.logistics.ct.entity.VeHqCheckitem;
import org.edu.modules.logistics.ct.mapper.VeHqCheckMapper;
import org.edu.modules.logistics.ct.entity.VeHqCheck;
import org.edu.modules.logistics.ct.dto.VeHqCheckDto;
import org.edu.modules.logistics.ct.service.VeHqCheckService;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Constants;
import org.apache.ibatis.annotations.Param;
import org.edu.modules.logistics.ct.service.VeHqCheckitemService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;


/**
 * 实现类
 */
@Service
public class VeHqCheckServiceImpl extends BaseServiceImpl<VeHqCheck, VeHqCheckMapper> implements VeHqCheckService {

    @Autowired
    private VeHqCheckitemService veHouqinCheckitemService;

    public IPage<VeHqCheckDto> pageDto(BasePageDto page, @Param(Constants.WRAPPER) Wrapper queryWrapper){
        return mapper.pageDto(page,queryWrapper);
    }
    /**
    * 查询实体类
    */
    public VeHqCheckDto findDtoById(String id) {
        VeHqCheck entity = super.findById(id);
        VeHqCheckDto dto = ConvertUtils.convertToDto(entity, VeHqCheckDto.class);

        LambdaQueryWrapper<VeHqCheckitem> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(VeHqCheckitem::getCheckId,dto.getId());
        List<VeHqCheckitem> checkItems = veHouqinCheckitemService.listByWrapper(lambdaQueryWrapper);
        dto.setItems(checkItems);
        return dto;
    }

    /**
    * 新增或者修改
    */
    @Transactional
    public void insertOrUpdate(VeHqCheckDto dto,Boolean isAdd){
        if(isAdd){
            super.insert(dto);
        }else{
            super.update(dto);
        }
        for (VeHqCheckitem item: dto.getItems()) {
            item.setCheckId(dto.getId());
        }
        veHouqinCheckitemService.insert(dto.getItems());

    }

}
